import {
  ComponentInfo,
  ComponentLabelItem,
  ComponentListResultModel,
  ComponentQueryPageParams,
  ComponentSelectParams,
  PreferredModel,
  RelationshipModel,
} from './model/componentModel';
import { deployHttp } from '/@/utils/http/axios';

enum Api {
  COMPONENT_BY_APP = '/v1/component',
  COMPONENT_DISPLAY_LIST = '/v1/component/componentPage',
  COMPONENT_ADD_UPDATE = '/v1/component/addOrUpdate',
  COMPONENT_LABEL_LIST = '/v1/component/getComponentLabelList',
  GET_PREFERRED_BY_COMPONENT = '/v1/image/getPreferredByComponent',
  DELETE_COMPONENT = '/v1/component/deleteComponent',
  GET_COMPONENT_RELATIONSHIP_LIST = '/v1/component/getComponentRelationshipList',
  GET_COMPONENT_LIST_BY_APP_ID = '/v1/component/getComponentListByAppId',
  ADD_OR_UPDATE_COMPONENT_RELATION_SHIP = '/v1/component/addOrUpdateComponentRelationship',
  DELETE_COMPONENT_RELATION_SHIP = '/v1/component/deleteComponentRelationship',
  COMPONETNT_RELATION_SHIP = '/v1/component/getComponentRelationshipForMap',
}

export const getComponentByApp = (params: ComponentSelectParams) => {
  // 获取应用
  if (params.appId) {
    return deployHttp.get<ComponentInfo[]>({
      url: Api.COMPONENT_BY_APP + '/' + params.appId,
      params,
    });
  }
};

export const getComponentByPage = (params: ComponentQueryPageParams) => {
  // 分页获取组件
  return deployHttp.post<ComponentListResultModel>({
    url: Api.COMPONENT_DISPLAY_LIST,
    params,
  });
};

export const addOrUpdateComponent = (params: ComponentInfo) => {
  // 添加组件
  return deployHttp.post<string>({ url: Api.COMPONENT_ADD_UPDATE, params });
};

export const getComponentLabelList = () => {
  return deployHttp.get<ComponentLabelItem[]>({ url: Api.COMPONENT_LABEL_LIST });
};

export const getPreferredByComponent = (params: { componentId: string; appId: string; id?: string }) => {
  return deployHttp.get<PreferredModel>({ url: Api.GET_PREFERRED_BY_COMPONENT, params });
};

export const deleteComponent = (param: string) => {
  const params = { id: param };
  return deployHttp.get<string>({ url: Api.DELETE_COMPONENT, params });
};

export const getComponentRelationshipList = async (params: { appId: string }) => {
  const result = await deployHttp.get<RelationshipModel>({ url: Api.GET_COMPONENT_RELATIONSHIP_LIST, params });
  result.upstreamComponentName?.forEach((item) => {
    result.upstreamComponentNameTableData = result.upstreamComponentNameTableData + item + ';';
  });
  result.downstreamComponentName?.forEach((item) => {
    result.downstreamComponentNameTableData = result.downstreamComponentNameTableData + item + ';';
  });
  return result;
};
export const getComponentList = (params: { appId: string }) => {
  return deployHttp.get<{ name: string; id: string; disabled: boolean }[]>({
    url: Api.GET_COMPONENT_LIST_BY_APP_ID,
    params,
  });
};
export const getComponentListWithoutCurrentComponent = async (params: { appId: string; current: string }) => {
  const result = await deployHttp.get<{ name: string; id: string }[]>({
    url: Api.GET_COMPONENT_LIST_BY_APP_ID,
    params,
  });
  const temp = [];
  result.forEach((item) => {
    if (item.id != params.current) {
      temp.push(item);
    }
  });
  return temp;
};

export const addOrUpdateComponentRelationship = async (params: RelationshipModel) => {
  return await deployHttp.post<string>({
    url: Api.ADD_OR_UPDATE_COMPONENT_RELATION_SHIP,
    params,
  });
};

export const deleteComponentRelationship = async (params: { appId: string; componentId: string }) => {
  return await deployHttp.get<string>({
    url: Api.DELETE_COMPONENT_RELATION_SHIP,
    params,
  });
};

export const getComponentRelationshipForMap = async (params: { appId: string }) => {
  return await deployHttp.get({
    url: Api.COMPONETNT_RELATION_SHIP,
    params,
  });
};
